package com.zkflzl.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkflzl.store.common.BaseResponse;
import com.zkflzl.store.common.ErrorCode;
import com.zkflzl.store.common.ResultUtils;
import com.zkflzl.store.mapper.ShopMapper;
import com.zkflzl.store.model.dto.shop.ShopAddRequest;
import com.zkflzl.store.model.dto.shop.ShopListRequest;
import com.zkflzl.store.model.entity.Shop;
import com.zkflzl.store.model.entity.User;
import com.zkflzl.store.service.ShopService;
import com.zkflzl.store.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

import static com.zkflzl.store.constant.UserConstant.ADMIN_ROLE;

/**
* @author 21582
* @description 针对表【tb_shop(门店表)】的数据库操作Service实现
* @createDate 2024-03-19 16:47:24
*/
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {
    @Resource
    private UserService userService;

    @Override
    public BaseResponse addShop(ShopAddRequest shopAddRequest, HttpServletRequest request) {
        // 权限校验
        User user = userService.getLoginUser(request);
        if(!Objects.equals(user.getUserRole(), ADMIN_ROLE)){
            return ResultUtils.error(ErrorCode.NO_AUTH_ERROR);
        }

        // 数据填充
        String userId = shopAddRequest.getUserId();
        String name = shopAddRequest.getName();
        String addr = shopAddRequest.getAddr();
        Shop shop = new Shop();
        shop.setUserId(user.getId());
        shop.setName(name);
        shop.setAddr(addr);
        shop.setUserId(Long.valueOf(userId));

        // 保存
        boolean save = this.save(shop);

        if (save) {
            return ResultUtils.success("添加成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_ERROR, "添加失败");
    }

    @Override
    public BaseResponse delShop(String shopId, HttpServletRequest request) {
        // 权限校验
        User user = userService.getLoginUser(request);
        if(!Objects.equals(user.getUserRole(), ADMIN_ROLE)){
            return ResultUtils.error(ErrorCode.NO_AUTH_ERROR);
        }

        // 删除
        boolean remove = this.removeById(shopId);
        if (remove) {
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_ERROR, "店铺不存在");
    }

    @Override
    public BaseResponse selShop(ShopListRequest shopListRequest, HttpServletRequest request) {
        if(!userService.isAdmin(request)){
            return ResultUtils.error(ErrorCode.NO_AUTH_ERROR,"无权限访问");
        }
        String shopId = shopListRequest.getShopId();
        long current = shopListRequest.getCurrent();
        long pageSize = shopListRequest.getPageSize();
        String sortField = shopListRequest.getSortField();
        String sortOrder = shopListRequest.getSortOrder();


        if(!StringUtils.isBlank(shopId)){
            Shop shop = this.getById(shopId);
            return ResultUtils.success(shop);
        }

        Page<Shop> page = new Page<>(current, pageSize);
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isBlank(sortField) && !StringUtils.isBlank(sortOrder)){
            queryWrapper.orderBy(true, "asc".equals(sortOrder), sortField);
        }

        Page<Shop> shopPage = this.page(page, queryWrapper);
        return ResultUtils.success(shopPage);
    }
}




